package com.fwtai.service.api;

import com.alibaba.fastjson2.JSONObject;
import com.fwtai.bean.PageFormData;
import com.fwtai.bean.UploadFile;
import com.fwtai.bean.UploadObject;
import com.fwtai.service.AsyncService;
import com.fwtai.tool.ToolClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 独立处理文件上传
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2021-09-14 11:46:52
 * @官网 <url>http://www.lilf.top</url>
*/
@Service
public class UploadService{

  @Value("${upload_dir_linux}")
  private String detailsDir;

  private final AsyncService asyncService;

  public UploadService(final AsyncService asyncService){
    this.asyncService = asyncService;
  }

  //处理身份证人像面、国徽面的上传
  public String idCard(final HttpServletRequest request){
    return handlerUpload(ToolClient.uploadFile(request,detailsDir,"/id_card",false,2,4,true),true);
  }

  //处理门店申请时的协议图片的上传
  public String protocol(final HttpServletRequest request){
    return handlerUpload(ToolClient.uploadFile(request,detailsDir,"/protocol",false,1,4,true),true);
  }

  //处理营业执照的上传
  public String license(final HttpServletRequest request){
    return handlerUpload(ToolClient.uploadFile(request,detailsDir,"/license",false,1,6,true),true);
  }

  /**
   * 处理并返回json格式数据
   * @param verify 是否验证必填
   * @param keys 是否是多个文件上传的name字段,如device_image,即一个文件域|单个文件域里含多文件上传,多个时前端传的key格式为device_image0为文件1;device_image1为文件2;device_image2为文件3
   * @return
  */
  private String handlerUpload(final UploadObject uploadObject,final boolean verify,final String... keys){
    final String errorMsg = uploadObject.getErrorMsg();
    if(errorMsg != null) return ToolClient.createJsonFail(errorMsg);
    final ArrayList<UploadFile> listFiles = uploadObject.getListFiles();
    if(verify){
      if(listFiles == null || listFiles.size() <= 0) return ToolClient.createJsonFail("请选择上传文件");
    }
    if(keys.length == 0){
      final PageFormData formData = new PageFormData();
      listFiles.forEach(uploadFile -> {
        final JSONObject json = new JSONObject(3);
        json.put("fullPath",uploadFile.getFullPath());
        json.put("urlFile",uploadFile.getUrlFile());
        json.put("originalName",uploadFile.getOriginalName());
        formData.put(uploadFile.getName(),json.toJSONString());
      });
      return ToolClient.queryJson(formData);
    }else{
      final ArrayList<String> listName = new ArrayList<>(listFiles.size());
      listFiles.forEach(uploadFile -> listName.add(uploadFile.getName()));
      //list分组
      final Map<String,List<String>> collects = listName.stream().collect(Collectors.groupingBy(itemValue -> {
        return itemValue.substring(0,itemValue.length() - 1);
      }));
      final PageFormData formData = new PageFormData();
      asyncService.handlerFile(formData,listFiles,collects,keys);//单个文件域里含多文件上传
      return ToolClient.queryJson(formData);
    }
  }
}